package func;
import node.*;
import java.util.*;

public class PolyRankFunction implements RankFunction {
	
	private int[][] map;
	
	public PolyRankFunction(int[][] map) {
		this.map = map;
	}

	public void sortByRank(Node[] pop) {
		Comparator<Node> comp = new 
			Comparator<Node>() {
				public int compare(Node n1, Node n2) {
					return applyScoreFunction(n1) - applyScoreFunction(n2);
				}
			};
		
		Arrays.sort(pop, comp);
	}
	
	public int applyScoreFunction(Node root) {
		int dif = 0;
		for(int i = 0; i < map.length; i++) {
			int v = root.evaluate(new int[]{map[i][0], map[i][1]});
			dif = dif + Math.abs(v - map[i][2]);
		}
		
		if(dif < 0)
			dif = Integer.MAX_VALUE;
				
		return dif;
	}
}
